package org.xbill.DNS;

import java.io.IOException;
import java.io.Serializable;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.NoSuchElementException;
import java.util.Set;
import java.util.TreeMap;

public class Zone
  implements Serializable
{
  public static final int PRIMARY = 1;
  public static final int SECONDARY = 2;
  private static final long serialVersionUID = 1661426914L;
  private RRset NS;
  private SOARecord SOA;
  private Map data;
  private int dclass = 1;
  private boolean hasWild;
  private Name origin;
  private Object originNode;

  public Zone(Name paramName, int paramInt, String paramString)
    throws IOException, ZoneTransferException
  {
    ZoneTransferIn localZoneTransferIn = ZoneTransferIn.newAXFR(paramName, paramString, null);
    localZoneTransferIn.setDClass(paramInt);
    fromXFR(localZoneTransferIn);
  }

  public Zone(Name paramName, String paramString)
    throws IOException
  {
    this.data = new TreeMap();
    if (paramName == null)
      throw new IllegalArgumentException("no zone name specified");
    Master localMaster = new Master(paramString, paramName);
    this.origin = paramName;
    while (true)
    {
      Record localRecord = localMaster.nextRecord();
      if (localRecord == null)
      {
        validate();
        return;
      }
      maybeAddRecord(localRecord);
    }
  }

  public Zone(Name paramName, Record[] paramArrayOfRecord)
    throws IOException
  {
    this.data = new TreeMap();
    if (paramName == null)
      throw new IllegalArgumentException("no zone name specified");
    this.origin = paramName;
    int i = 0;
    while (true)
    {
      if (i >= paramArrayOfRecord.length)
      {
        validate();
        return;
      }
      maybeAddRecord(paramArrayOfRecord[i]);
      ++i;
    }
  }

  public Zone(ZoneTransferIn paramZoneTransferIn)
    throws IOException, ZoneTransferException
  {
    fromXFR(paramZoneTransferIn);
  }

  /**
   * @deprecated
   */
  private void addRRset(Name paramName, RRset paramRRset)
  {
    monitorenter;
    try
    {
    }
    finally
    {
      while (true)
      {
        int j;
        while (true)
        {
          RRset localRRset;
          while (true)
          {
            Object localObject2;
            int i;
            while (true)
            {
              List localList;
              monitorexit;
              throw localObject1;
              if (((RRset)localList.get(j)).getType() != i)
                break label212;
              localList.set(j, paramRRset);
            }
            localRRset = (RRset)localObject2;
            if (localRRset.getType() != i)
              break;
            this.data.put(paramName, paramRRset);
          }
          LinkedList localLinkedList = new LinkedList();
          localLinkedList.add(localRRset);
          localLinkedList.add(paramRRset);
          this.data.put(paramName, localLinkedList);
        }
        label212: ++j;
      }
    }
  }

  /**
   * @deprecated
   */
  private RRset[] allRRsets(Object paramObject)
  {
    RRset localRRset;
    RRset[] arrayOfRRset;
    monitorenter;
    try
    {
      if (paramObject instanceof List)
      {
        List localList = (List)paramObject;
        arrayOfRRset = (RRset[])localList.toArray(new RRset[localList.size()]);
        monitorexit;
        return arrayOfRRset;
      }
      localRRset = (RRset)paramObject;
      arrayOfRRset = new RRset[1];
    }
    finally
    {
      monitorexit;
    }
  }

  /**
   * @deprecated
   */
  private Object exactName(Name paramName)
  {
    monitorenter;
    try
    {
      Object localObject2 = this.data.get(paramName);
      monitorexit;
      return localObject2;
    }
    finally
    {
      localObject1 = finally;
      monitorexit;
      throw localObject1;
    }
  }

  /**
   * @deprecated
   */
  private RRset findRRset(Name paramName, int paramInt)
  {
    RRset localRRset;
    Object localObject3;
    monitorenter;
    try
    {
      Object localObject2 = exactName(paramName);
      if (localObject2 == null)
      {
        localObject3 = null;
        monitorexit;
        return localObject3;
      }
      localRRset = oneRRset(localObject2, paramInt);
    }
    finally
    {
      monitorexit;
    }
  }

  private void fromXFR(ZoneTransferIn paramZoneTransferIn)
    throws IOException, ZoneTransferException
  {
    this.data = new TreeMap();
    this.origin = paramZoneTransferIn.getName();
    Iterator localIterator = paramZoneTransferIn.run().iterator();
    while (true)
    {
      if (!(localIterator.hasNext()))
      {
        if (paramZoneTransferIn.isAXFR())
          break;
        throw new IllegalArgumentException("zones can only be created from AXFRs");
      }
      maybeAddRecord((Record)localIterator.next());
    }
    validate();
  }

  /**
   * @deprecated
   */
  private SetResponse lookup(Name paramName, int paramInt)
  {
    monitorenter;
    try
    {
    }
    finally
    {
      int i;
      int j;
      int k;
      int l;
      int i1;
      Name localName;
      int i4;
      while (true)
      {
        while (true)
        {
          while (true)
          {
            Object localObject2;
            while (true)
            {
              while (true)
              {
                int i2;
                RRset localRRset4;
                SetResponse localSetResponse4;
                monitorexit;
                throw localObject1;
                if ((i1 != 0) && (paramInt == 255))
                {
                  SetResponse localSetResponse2 = new SetResponse(6);
                  RRset[] arrayOfRRset = allRRsets(localObject2);
                  int i3 = 0;
                  while (true)
                  {
                    while (i3 >= arrayOfRRset.length)
                      localObject3 = localSetResponse2;
                    localSetResponse2.addRRset(arrayOfRRset[i3]);
                    ++i3;
                  }
                }
                if (i1 == 0)
                  break label302;
                RRset localRRset2 = oneRRset(localObject2, paramInt);
                if (localRRset2 == null)
                  break;
                SetResponse localSetResponse1 = new SetResponse(6);
                localSetResponse1.addRRset(localRRset2);
                localObject3 = localSetResponse1;
              }
              RRset localRRset3 = oneRRset(localObject2, 5);
              if (localRRset3 == null)
                break label332;
              localObject3 = new SetResponse(4, localRRset3);
            }
            label302: RRset localRRset1 = oneRRset(localObject2, 39);
            if (localRRset1 == null)
              break;
            localObject3 = new SetResponse(5, localRRset1);
          }
          label332: if (i1 == 0)
            break label443;
          localObject3 = SetResponse.ofType(2);
        }
        Object localObject4 = exactName(paramName.wild(i4 + 1));
        if (localObject4 == null)
          break;
        RRset localRRset5 = oneRRset(localObject4, paramInt);
        if (localRRset5 == null)
          break;
        SetResponse localSetResponse3 = new SetResponse(6);
        localSetResponse3.addRRset(localRRset5);
        Object localObject3 = localSetResponse3;
      }
      while (true)
      {
        while (true)
        {
          do
            while (true)
            {
              while (true)
                if (i4 < i - j);
              if (k == j)
                l = 1;
              while (true)
              {
                while (true)
                {
                  while (true)
                  {
                    if (k != i)
                      break label455;
                    i1 = 1;
                  }
                  label443: ++k;
                }
                l = 0;
              }
              label455: i1 = 0;
            }
          while (i1 == 0);
          localName = paramName;
        }
        ++i4;
      }
    }
  }

  private final void maybeAddRecord(Record paramRecord)
    throws IOException
  {
    int i = paramRecord.getType();
    Name localName = paramRecord.getName();
    if ((i == 6) && (!(localName.equals(this.origin))))
      throw new IOException("SOA owner " + localName + " does not match zone origin " + this.origin);
    if (localName.subdomain(this.origin))
      addRecord(paramRecord);
  }

  private void nodeToString(StringBuffer paramStringBuffer, Object paramObject)
  {
    Iterator localIterator2;
    RRset[] arrayOfRRset = allRRsets(paramObject);
    int i = 0;
    if (i >= arrayOfRRset.length)
      return;
    RRset localRRset = arrayOfRRset[i];
    Iterator localIterator1 = localRRset.rrs();
    if (!(localIterator1.hasNext()))
      localIterator2 = localRRset.sigs();
    while (true)
    {
      while (true)
      {
        while (true)
        {
          if (localIterator2.hasNext())
            break label97;
          ++i;
        }
        paramStringBuffer.append(localIterator1.next() + "\n");
      }
      label97: paramStringBuffer.append(localIterator2.next() + "\n");
    }
  }

  /**
   * @deprecated
   */
  private RRset oneRRset(Object paramObject, int paramInt)
  {
    monitorenter;
    if (paramInt == 255);
    try
    {
      throw new IllegalArgumentException("oneRRset(ANY)");
    }
    finally
    {
      List localList;
      int j;
      monitorexit;
      throw localObject2;
      if (paramObject instanceof List)
      {
        localList = (List)paramObject;
        j = 0;
      }
      while (true)
      {
        Object localObject1;
        int k = localList.size();
        if (j >= k)
          localObject1 = null;
        while (true)
        {
          RRset localRRset1;
          int i;
          do
          {
            while (true)
            {
              monitorexit;
              return localObject1;
              RRset localRRset2 = (RRset)localList.get(j);
              if (localRRset2.getType() != paramInt)
                break label120;
              localObject1 = localRRset2;
            }
            localRRset1 = (RRset)paramObject;
            i = localRRset1.getType();
          }
          while (i != paramInt);
          localObject1 = localRRset1;
        }
        label120: ++j;
      }
    }
  }

  /**
   * @deprecated
   */
  private void removeRRset(Name paramName, int paramInt)
  {
    Object localObject2;
    monitorenter;
    try
    {
      localObject2 = this.data.get(paramName);
      if (localObject2 == null)
      {
        monitorexit;
        return;
      }
      if (localObject2 instanceof List);
    }
    finally
    {
      while (true)
      {
        do
          while (true)
          {
            List localList;
            int i;
            monitorexit;
            throw localObject1;
            ++i;
          }
        while (((RRset)localObject2).getType() != paramInt);
        this.data.remove(paramName);
      }
    }
  }

  private void validate()
    throws IOException
  {
    this.originNode = exactName(this.origin);
    if (this.originNode == null)
      throw new IOException(this.origin + ": no data specified");
    RRset localRRset = oneRRset(this.originNode, 6);
    if ((localRRset == null) || (localRRset.size() != 1))
      throw new IOException(this.origin + ": exactly 1 SOA must be specified");
    this.SOA = ((SOARecord)localRRset.rrs().next());
    this.NS = oneRRset(this.originNode, 2);
    if (this.NS == null)
      throw new IOException(this.origin + ": no NS set specified");
  }

  public Iterator AXFR()
  {
    return new ZoneIterator(this, true);
  }

  public void addRRset(RRset paramRRset)
  {
    addRRset(paramRRset.getName(), paramRRset);
  }

  // ERROR //
  public void addRecord(Record paramRecord)
  {
    // Byte code:
    //   0: aload_1
    //   1: invokevirtual 226	org/xbill/DNS/Record:getName	()Lorg/xbill/DNS/Name;
    //   4: astore_2
    //   5: aload_1
    //   6: invokevirtual 298	org/xbill/DNS/Record:getRRsetType	()I
    //   9: istore_3
    //   10: aload_0
    //   11: monitorenter
    //   12: aload_0
    //   13: aload_2
    //   14: iload_3
    //   15: invokespecial 300	org/xbill/DNS/Zone:findRRset	(Lorg/xbill/DNS/Name;I)Lorg/xbill/DNS/RRset;
    //   18: astore 5
    //   20: aload 5
    //   22: ifnonnull +19 -> 41
    //   25: aload_0
    //   26: aload_2
    //   27: new 130	org/xbill/DNS/RRset
    //   30: dup
    //   31: aload_1
    //   32: invokespecial 302	org/xbill/DNS/RRset:<init>	(Lorg/xbill/DNS/Record;)V
    //   35: invokespecial 295	org/xbill/DNS/Zone:addRRset	(Lorg/xbill/DNS/Name;Lorg/xbill/DNS/RRset;)V
    //   38: aload_0
    //   39: monitorexit
    //   40: return
    //   41: aload 5
    //   43: aload_1
    //   44: invokevirtual 305	org/xbill/DNS/RRset:addRR	(Lorg/xbill/DNS/Record;)V
    //   47: goto -9 -> 38
    //   50: astore 4
    //   52: aload_0
    //   53: monitorexit
    //   54: aload 4
    //   56: athrow
    //
    // Exception table:
    //   from	to	target	type
    //   12	54	50	finally
  }

  public RRset findExactMatch(Name paramName, int paramInt)
  {
    RRset localRRset;
    Object localObject = exactName(paramName);
    if (localObject == null)
      localRRset = null;
    while (true)
    {
      return localRRset;
      localRRset = oneRRset(localObject, paramInt);
    }
  }

  public SetResponse findRecords(Name paramName, int paramInt)
  {
    return lookup(paramName, paramInt);
  }

  public int getDClass()
  {
    return this.dclass;
  }

  public RRset getNS()
  {
    return this.NS;
  }

  public Name getOrigin()
  {
    return this.origin;
  }

  public SOARecord getSOA()
  {
    return this.SOA;
  }

  public Iterator iterator()
  {
    return new ZoneIterator(this, false);
  }

  // ERROR //
  public void removeRecord(Record paramRecord)
  {
    // Byte code:
    //   0: aload_1
    //   1: invokevirtual 226	org/xbill/DNS/Record:getName	()Lorg/xbill/DNS/Name;
    //   4: astore_2
    //   5: aload_1
    //   6: invokevirtual 298	org/xbill/DNS/Record:getRRsetType	()I
    //   9: istore_3
    //   10: aload_0
    //   11: monitorenter
    //   12: aload_0
    //   13: aload_2
    //   14: iload_3
    //   15: invokespecial 300	org/xbill/DNS/Zone:findRRset	(Lorg/xbill/DNS/Name;I)Lorg/xbill/DNS/RRset;
    //   18: astore 5
    //   20: aload 5
    //   22: ifnonnull +8 -> 30
    //   25: aload_0
    //   26: monitorexit
    //   27: goto +51 -> 78
    //   30: aload 5
    //   32: invokevirtual 278	org/xbill/DNS/RRset:size	()I
    //   35: iconst_1
    //   36: if_icmpne +33 -> 69
    //   39: aload 5
    //   41: invokevirtual 319	org/xbill/DNS/RRset:first	()Lorg/xbill/DNS/Record;
    //   44: aload_1
    //   45: invokevirtual 320	org/xbill/DNS/Record:equals	(Ljava/lang/Object;)Z
    //   48: ifeq +21 -> 69
    //   51: aload_0
    //   52: aload_2
    //   53: iload_3
    //   54: invokespecial 322	org/xbill/DNS/Zone:removeRRset	(Lorg/xbill/DNS/Name;I)V
    //   57: aload_0
    //   58: monitorexit
    //   59: goto +19 -> 78
    //   62: astore 4
    //   64: aload_0
    //   65: monitorexit
    //   66: aload 4
    //   68: athrow
    //   69: aload 5
    //   71: aload_1
    //   72: invokevirtual 325	org/xbill/DNS/RRset:deleteRR	(Lorg/xbill/DNS/Record;)V
    //   75: goto -18 -> 57
    //   78: return
    //
    // Exception table:
    //   from	to	target	type
    //   12	66	62	finally
    //   69	75	62	finally
  }

  /**
   * @deprecated
   */
  public String toMasterFile()
  {
    StringBuffer localStringBuffer;
    Map.Entry localEntry;
    monitorenter;
    try
    {
      Iterator localIterator = this.data.entrySet().iterator();
      localStringBuffer = new StringBuffer();
      nodeToString(localStringBuffer, this.originNode);
      do
      {
        if (!(localIterator.hasNext()))
        {
          String str = localStringBuffer.toString();
          monitorexit;
          return str;
        }
        localEntry = (Map.Entry)localIterator.next();
      }
      while (this.origin.equals(localEntry.getKey()));
    }
    finally
    {
      monitorexit;
    }
  }

  public String toString()
  {
    return toMasterFile();
  }

  class ZoneIterator
  implements Iterator
  {
    private int count;
    private RRset[] current;
    private boolean wantLastSOA;
    private Iterator zentries;

    ZoneIterator(, boolean paramBoolean)
    {
      monitorenter;
      try
      {
        return;
      }
      finally
      {
        monitorexit;
        throw localObject;
        int k = arrayOfRRset1[i].getType();
        if (k == 6)
          this.current[0] = arrayOfRRset1[i];
        while (true)
        {
          while (true)
          {
            while (true)
              ++i;
            if (k != 2)
              break;
            this.current[1] = arrayOfRRset1[i];
          }
          RRset[] arrayOfRRset2 = this.current;
          int l = j + 1;
          arrayOfRRset2[j] = arrayOfRRset1[i];
          j = l;
        }
      }
    }

    public boolean hasNext()
    {
      int i;
      if ((this.current == null) && (!(this.wantLastSOA)))
        i = 0;
      while (true)
      {
        return i;
        i = 1;
      }
    }

    public Object next()
    {
      Object localObject;
      if (!(hasNext()))
        throw new NoSuchElementException();
      if (this.current == null)
      {
        this.wantLastSOA = false;
        localObject = Zone.access$3(this.this$0, Zone.access$1(this.this$0), 6);
        return localObject;
      }
      RRset[] arrayOfRRset1 = this.current;
      int i = this.count;
      this.count = (i + 1);
      RRset localRRset = arrayOfRRset1[i];
      if (this.count == this.current.length)
      {
        this.current = null;
        if (this.zentries.hasNext())
          break label104;
      }
      while (true)
      {
        label104: RRset[] arrayOfRRset2;
        do
        {
          Map.Entry localEntry;
          do
          {
            while (true)
              localObject = localRRset;
            localEntry = (Map.Entry)this.zentries.next();
          }
          while (localEntry.getKey().equals(Zone.access$4(this.this$0)));
          arrayOfRRset2 = Zone.access$2(this.this$0, localEntry.getValue());
        }
        while (arrayOfRRset2.length == 0);
        this.current = arrayOfRRset2;
        this.count = 0;
      }
    }

    public void remove()
    {
      throw new UnsupportedOperationException();
    }
  }
}